//
//  GEContainerBaseViewController.m
//  GETransformVC
//
//  Created by Grass on 15/8/13.
//  Copyright (c) 2015年 OFashioin. All rights reserved.
//

#import "GEContainerBaseViewController.h"

static const CGFloat kButtonHeight = 40.f;
static const CGFloat kViewMargin = 20.f;

@interface GEContainerBaseViewController ()

@property (nonatomic, strong, readwrite) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *firstButton;
@property (nonatomic, strong) UIButton *secondButton;
@property (nonatomic, strong) UIButton *thirdButton;

@end

@implementation GEContainerBaseViewController

- (void)loadView {
    [super loadView];
    
    [self buildTitleLabel];
    [self buildFirstButton];
    [self buildSecondButton];
    [self buildThirdButton];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    CGSize viewBoundsSize = self.view.bounds.size;
    self.titleLabel.frame = CGRectMake(0.f,
                                       100.f,
                                       viewBoundsSize.width,
                                       40.f);
    self.firstButton.frame = CGRectMake(0.f,
                                        CGRectGetMaxY(self.titleLabel.frame) + kViewMargin,
                                        viewBoundsSize.width,
                                        kButtonHeight);
    self.secondButton.frame = CGRectMake(0.f,
                                         CGRectGetMaxY(self.firstButton.frame) + kViewMargin,
                                         viewBoundsSize.width,
                                         kButtonHeight);
    self.thirdButton.frame = CGRectMake(0.f,
                                        CGRectGetMaxY(self.secondButton.frame) + kViewMargin,
                                        viewBoundsSize.width,
                                        kButtonHeight);
}

#pragma mark - Public
- (GETransformType)transformType {
    return kGETransformTypeToNone;
}

#pragma mark - Action
- (void)didClickButton:(UIButton *)sender {
    if (!self.delegate || ![self.delegate respondsToSelector:@selector(viewController:didSelectedTransformType:)]) {
        return;
    }
    GETransformType type = kGETransformTypeToNone;
    if (sender == self.firstButton) {
        type = kGETransformTypeToFirst;
    } else if (sender == self.secondButton) {
        type = kGETransformTypeToSecond;
    } else if (sender == self.thirdButton) {
        type = kGETransformTypeToThird;
    }
    [self.delegate viewController:self didSelectedTransformType:type];
}

#pragma mark - Private
#pragma mark Builder
- (void)buildTitleLabel {
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.backgroundColor = [UIColor grayColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.titleLabel];
}

- (void)buildFirstButton {
    self.firstButton = [self createButtonWithTitle:@"FIRST"];
    [self.view addSubview:self.firstButton];
}

- (void)buildSecondButton {
    self.secondButton = [self createButtonWithTitle:@"SECOND"];
    [self.view addSubview:self.secondButton];
}

- (void)buildThirdButton {
    self.thirdButton = [self createButtonWithTitle:@"THIRD"];
    [self.view addSubview:self.thirdButton];
}

#pragma mark Helper
- (UIButton *)createButtonWithTitle:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self
                         action:@selector(didClickButton:)
               forControlEvents:UIControlEventTouchUpInside];
    return button;
}


@end
